package com.teacherprofile.manage.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacherprofile.common.constant.AcademicPapersConstants;
import com.teacherprofile.common.constant.BusinessConstants;
import com.teacherprofile.common.constant.SocialParttimeConstants;
import com.teacherprofile.common.core.domain.entity.SysUser;
import com.teacherprofile.common.domain.AcademicPapers;
import com.teacherprofile.common.enums.CheckStatus;
import com.teacherprofile.common.exception.ServiceException;
import com.teacherprofile.common.utils.DateUtils;
import com.teacherprofile.framework.manager.AsyncManager;
import com.teacherprofile.framework.manager.factory.AsyncFactory;
import com.teacherprofile.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.teacherprofile.manage.mapper.SocialParttimeMapper;
import com.teacherprofile.common.domain.SocialParttime;
import com.teacherprofile.manage.service.ISocialParttimeService;
import org.springframework.util.CollectionUtils;

/**
 * 社会兼职Service业务层处理
 * 
 * @author qvtu
 * @date 2025-05-15
 */
@Service
public class SocialParttimeServiceImpl extends ServiceImpl<SocialParttimeMapper,SocialParttime> implements ISocialParttimeService
{
    @Autowired
    private SocialParttimeMapper socialParttimeMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询社会兼职
     * 
     * @param id 社会兼职主键
     * @return 社会兼职
     */
    @Override
    public SocialParttime selectSocialParttimeById(Long id)
    {
        return socialParttimeMapper.selectSocialParttimeById(id);
    }

    /**
     * 查询社会兼职列表
     * 
     * @param socialParttime 社会兼职
     * @return 社会兼职
     */
    @Override
    public List<SocialParttime> selectSocialParttimeList(SocialParttime socialParttime)
    {
        return socialParttimeMapper.selectSocialParttimeList(socialParttime);
    }

    /**
     * 新增社会兼职
     * 
     * @param socialParttime 社会兼职
     * @return 结果
     */
    @Override
    public boolean insertSocialParttime(SocialParttime socialParttime)
    {
        String userName = socialParttime.getUserName();
        if (ObjectUtils.isEmpty(userName)) {
            throw new RuntimeException("教职工号不能为空");
        }
        // 根据教职工号查询用户
        SysUser sysUser = userService.selectUserByUserName(userName);
        if (ObjectUtils.isEmpty(sysUser)) {
            // 用户不存在
            throw new RuntimeException("教职工号不存在");
        }
        // 用户存在，设置用户的userId与部门id，保存
        socialParttime.setUserId(sysUser.getUserId());
        socialParttime.setDeptId(sysUser.getDeptId());
        return save(socialParttime);
    }

    /**
     * 修改社会兼职
     * 
     * @param socialParttime 社会兼职
     * @return 结果
     */
    @Override
    public boolean updateSocialParttime(SocialParttime socialParttime)
    {
        return updateById(socialParttime);
    }

    /**
     * 批量删除社会兼职
     * 
     * @param ids 需要删除的社会兼职主键
     * @return 结果
     */
    @Override
    public int deleteSocialParttimeByIds(Long[] ids)
    {
        return socialParttimeMapper.deleteSocialParttimeByIds(ids);
    }


    /**
     * 导入数据
     * @param socialParttimes
     * @return
     */
    @Override
    public String importData(List<SocialParttime> socialParttimes) {
        if (CollectionUtils.isEmpty(socialParttimes)) {
            throw new ServiceException("导入数据不能为空！");
        }
        // 查询导入的教职工号去重集合
        Set<String> userNameList = socialParttimes.stream().map(papers -> papers.getUserName()).collect(Collectors.toSet());
        // 根据去重集合查询对应的用户
        List<SysUser> userList = userService.selectUserByUserNames(userNameList);
        if (ObjectUtils.isEmpty(userList)) {
            throw new ServiceException("导入数据中教职工号不存在！");
        }
        // 将列表转成Map，key为教职工号，value为SysUser对象
        Map<String, SysUser> userMap = userList.stream().collect(Collectors.toMap(SysUser::getUserName, item -> item));
        for (SocialParttime socialParttime : socialParttimes) {
            // 填充用户的userId与deptId
            SysUser sysUser = userMap.get(socialParttime.getUserName());
            socialParttime.setUserId(sysUser.getUserId());
            socialParttime.setDeptId(sysUser.getDeptId());
        }
        return saveBatch(socialParttimes) ? "导入成功" : "导入失败";
    }

    /**
     * 审核
     * @param socialParttimes
     * @return
     */
    @Override
    public boolean check(SocialParttime socialParttimes) {
        if (ObjectUtils.isNull(socialParttimes)) { // 非法参数
            throw new ServiceException(BusinessConstants.BUSINESS_ERROR);
        }
        if (socialParttimes.getCheckStatus().equals(CheckStatus.PASS.getCode())) {
            // 审核通过
            // TODO 发送mq消息，存储数据
            //rabbitMqHelper.send(MqConstants.Exchange.SOFTWARE_COPYWRITE_EXCHANGE,
            //                    MqConstants.Key.SOFTWARE_COPYWRITE_PASS_KEY, softwareCopyrights);

            // 通知用户，审核通过
            AsyncManager.me().execute(AsyncFactory.sendPassMsgToOne(socialParttimes.getUserName(), SocialParttimeConstants.CHECKED_PASS));
        } else {
            // 审核不通过
            // TODO 发送mq消息，存储数据
            // 通知用户，审核不通过
            AsyncManager.me().execute(AsyncFactory.sendRejectMsgToOne(socialParttimes.getUserName(), SocialParttimeConstants.CHECKED_REJECT));
        }
        // 删除此条数据

        return updateById(socialParttimes);
    }
}
